package engine.newUi
{
	import newx.NxSprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import newx.com.config.StaticIni;
	import newx.StageInfo;
	import flash.text.TextFieldAutoSize;
	import flash.filters.GlowFilter;
	import flash.filters.BitmapFilterQuality;
	import com.greensock.TweenLite;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.display.Sprite;
	import com.greensock.easing.Linear;
	/**
	 *广播（滚动） 
	 * @author liuel
	 * 
	 */	
	public class BroadcastUI extends NxSprite
	{
		private static var _self:BroadcastUI;
		/**
		 *是否正在播放 
		 */		
		private var _isPlay:Boolean;
		/**广播的信息数组(只保存未播放的)**/
		private var _infoArr:Array=[];;
		
		private var _msgText:TextField;
		private var _textFormat	:TextFormat;
		
		private const SPEED:int =500 //
		private var _time:Timer;
		/**当前播放的字符串**/
		private var _currplayStr:String;
		//遮罩层
		private var sprite:Sprite;
		//遮罩层
		private var sprite1:Sprite;
		public static function getInstance():BroadcastUI
		{
			if(_self ==null)
			{
				_self = new BroadcastUI();
			}
			return _self;
		}
		private function init():void
		{
			this.y=20
			_textFormat=new TextFormat();
			_textFormat.size			=	24;
			_textFormat.bold 			=	true;
			_textFormat.color			=	StaticIni.getIniVar("Css_txt_ImportantMessageUI", "txtStr");
			
			_msgText = new TextField();
			_msgText.mouseEnabled	=	false;
			_msgText.selectable		=	false;
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	4;
			glow.quality			= 	BitmapFilterQuality.HIGH;
			_msgText.filters		=	[glow];
			_msgText.defaultTextFormat	=	_textFormat;
			_msgText.autoSize =TextFieldAutoSize.LEFT;
			_isPlay=false;
			sprite=new Sprite();
			addPanle();
			
		}
		public function BroadcastUI()
		{
			super();
			init();
		}
		private function computeTime(str:String):void
		{
			_isPlay =true;
			_msgText.x =StageInfo.rootW;
		
			_currplayStr =str;
			_msgText.htmlText = _currplayStr;
			var totaltime:Number = str.length*SPEED;
			var needtime:Number;
			if(totaltime/500<10)
			{
				needtime =10;
			}
			else
			{
				needtime =totaltime/500;
			}
			TweenLite.to(_msgText,needtime,{x:-_msgText.width,ease:Linear.easeNone,onComplete:onCompleteHandle})
			StageInfo.defaultSpace.addChild(this);
		}
		/**
		 *添加遮罩
		 * 
		 */		
		private function addPanle():void
		{
			sprite.graphics.beginFill(0xfff111,1.0);
			sprite.graphics.drawRect(StageInfo.defaultSpace.x+100,StageInfo.defaultSpace.y+20,StageInfo.rootW-200,50);
			sprite.graphics.endFill();
			
			StageInfo.defaultSpace.addChild(sprite);
			this.mask =sprite
			this.addChild(_msgText);
		}
		private function onTimerHandle():void
		{
			_msgText.htmlText = _currplayStr;
			
		}
		private function onCompleteHandle():void
		{
			_isPlay =false;
			clear();
//			//显示下一条
			showInfo("");
		}
		private function clear():void
		{
			_msgText.htmlText ="";
			hide();
		}
		/**
		 *显示一条广播数据 
		 * 
		 */		
		private function showBroadcastMsg():void
		{
			
			if(_infoArr.length>0)
			{
				var str:String = _infoArr.shift();   //取第一条数据
				computeTime(str);
			}
			
		}
		public function showInfo(str:String):void
		{
			if(str !="")
				_infoArr.push(str);
			if(!_isPlay)
				showBroadcastMsg()
			
		}
	}
}